package org.example.lease.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;

import org.example.lease.model.entities.LeaseAgreement;
import org.example.lease.model.enums.LeaseStatus;
import org.example.lease.model.result.Result;
import org.example.lease.service.LeaseAgreementService;
import org.example.lease.vo.agreement.AgreementQueryVo;
import org.example.lease.vo.agreement.AgreementVo;
import org.springframework.web.bind.annotation.*;

/**
 * @author 宋孟林
 * @description
 * @date: 2025/5/24 15:37
 */
@Tag(name = "租约信息管理")
@RestController
@Slf4j
@RequestMapping("/admin/agreement")
public class LeaseAgreementController {
    @Resource
    private LeaseAgreementService leaseAgreementService;

    @Operation(summary = "保存或修改租约信息")
    @PostMapping("saveOrUpdate")
    public Result<?> saveOrUpdate(@RequestBody LeaseAgreement leaseAgreement) {
        leaseAgreementService.saveOrUpdate(leaseAgreement);
        return Result.ok();
    }

    @Operation(summary = "根据条件分页查询租约列表")
    @GetMapping("/page")
    public Result<IPage<AgreementVo>> page(@RequestParam("current") Long current,
                                           @RequestParam("size") Long size,
                                           AgreementQueryVo agreementQueryVo) {
        Page<AgreementVo> page = new Page<>(current, size);
        IPage<AgreementVo> agreementVoIPage = leaseAgreementService.pageLeaseAgreementByQuery(page, agreementQueryVo);
        return Result.ok(agreementVoIPage);
    }

    @Operation(summary = "根据id查询租约信息")
    @GetMapping(name = "/getById")
    public Result<AgreementVo> getById(@RequestParam Long id) {
        AgreementVo agreementVo = leaseAgreementService.selectLeaseAgreementById(id);
        return Result.ok(agreementVo);
    }

    @Operation(summary = "根据id删除租约信息")
    @DeleteMapping("removeById")
    public Result<?> removeById(@RequestParam Long id) {
        leaseAgreementService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id更新租约状态")
    @PostMapping("/updateStatusById")
    public Result<?> updateStatusById(@RequestParam Long id, @RequestParam LeaseStatus status) {
        leaseAgreementService.update(null, Wrappers.lambdaUpdate(LeaseAgreement.class)
                .eq(LeaseAgreement::getId, id).set(LeaseAgreement::getStatus, status));
        return Result.ok();
    }
}
